package com.proton.carepatchtemp.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.base.BaseActivity;
import com.proton.carepatchtemp.constant.Codes;
import com.proton.carepatchtemp.utils.FormUtils;
import com.proton.carepatchtemp.utils.TastyUtils;
import com.proton.carepatchtemp.utils.eventbus.MainEvent;
import com.proton.carepatchtemp.utils.net.ConfigCenter;
import com.sdsmdg.tastytoast.TastyToast;

public class FeedBackActivity extends BaseActivity {

    boolean isNoBeauty = false;
    boolean isHaveBug = false;
    boolean isGoodPro = false;
    /**
     * 界面不够美观
     */
    private TextView mNoBeauty;
    /**
     * 软件有bug
     */
    private TextView mHaveBug;
    /**
     * 对产品不满意
     */
    private TextView mNoGoodPro;
    /**
     * 备注
     */
    private EditText mContent;
    /**
     * 发送
     */
    private Button mPost;
    private ImageButton mBtnBack;


    @Override
    public void dealEvent(MainEvent event) {
        if (null == event) {
            return;
        }
        int id = event.getId();
        switch (id) {
            case Codes.FEEDBACK_ADD:
                if (event.isSuccess()) {
                    TastyUtils.makeTextShort(R.string.feedback_success, TastyToast.SUCCESS);
                    init();
                } else {
                    TastyUtils.makeTextShort(event.getMsg(), TastyToast.ERROR);
                }
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed_back);
        initView();
        setListener();
    }

    @Override
    protected void setListener() {
        mBtnBack.setOnClickListener(v -> {
            finish();
        });
        mNoBeauty.setOnClickListener(view -> {
            isNoBeauty = !isNoBeauty;
            if (isNoBeauty) {
                mNoBeauty.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.checked, 0);
            } else {
                mNoBeauty.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.unchecked, 0);
            }
        });

        mHaveBug.setOnClickListener(view -> {
            isHaveBug = !isHaveBug;
            if (isHaveBug) {
                mHaveBug.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.checked, 0);
            } else {
                mHaveBug.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.unchecked, 0);
            }
        });

        mNoGoodPro.setOnClickListener(view -> {
            isGoodPro = !isGoodPro;
            if (isGoodPro) {
                mNoGoodPro.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.checked, 0);
            } else {
                mNoGoodPro.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.unchecked, 0);
            }
        });

        mPost.setOnClickListener(view -> {
            StringBuilder str = new StringBuilder();
            if (isGoodPro) {
                str.append(getResources().getString(R.string.feed_back_notgood) + ",");
            }
            if (isNoBeauty) {
                str.append(getResources().getString(R.string.feed_back_nobeauty) + ",");
            }
            if (isHaveBug) {
                str.append(getResources().getString(R.string.feed_back_havebug) + ",");
            }
            String content = FormUtils.getString(mContent);
            str.append(content);
            if (TextUtils.isEmpty(str)) {
                TastyUtils.makeTextShort(R.string.say_something, TastyToast.ERROR);

            } else {
                ConfigCenter.feedback(str.toString());
            }
        });
    }

    @Override
    public void onEventMainThread(MainEvent event) {
        dealOnEvent(event);
    }

    private void initView() {
        mNoBeauty = findViewById(R.id.no_beauty);
        mHaveBug = findViewById(R.id.have_bug);
        mNoGoodPro = findViewById(R.id.no_good_pro);
        mContent = findViewById(R.id.content);
        mPost = findViewById(R.id.bt_send_suggestion);
        mBtnBack = findViewById(R.id.btn_back);
    }

    private void init() {
        mNoBeauty.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.unchecked, 0);
        mHaveBug.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.unchecked, 0);
        mNoGoodPro.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.unchecked, 0);
        mContent.setText("");
        isNoBeauty = false;
        isHaveBug = false;
        isGoodPro = false;
    }

}
